package org.shuma.proxy.service.impl;


import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import org.shuma.proxy.dao.ProductProxyDao;
import org.shuma.proxy.facade.entity.ProductProxy;
import org.shuma.proxy.facade.Proxy;
import org.shuma.proxy.facade.entity.ProductProxyDTO;
import org.shuma.proxy.facade.entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (ProductProxy)表服务实现类
 *
 * @author makejava
 * @since 2024-06-18 11:59:11
 */
@Service("productProxyService")
@SofaService(interfaceType = Proxy.class, uniqueId = "${service.unique.id}", bindings = {@SofaServiceBinding(bindingType = "bolt")})
public class ProductProxyServiceImpl implements Proxy{
    @Resource
    private ProductProxyDao productProxyDao;
    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 通过ID查询单条数据
     * @return 实例对象
     */
    @Override
    public List<ProductProxy> queryById(Long productId) {
        return this.productProxyDao.queryById(productId);
    }

    /**
     * 分页查询
     *
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    public Page<ProductProxy> queryByPage(Long productId, PageRequest pageRequest) {
        ProductProxy productProxy = new ProductProxy();
        productProxy.setProductId(productId);
        long total = this.productProxyDao.count(productProxy);
        return new PageImpl<>(this.productProxyDao.queryAllByLimit(productId, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param productProxy 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean addProductProxy(ProductProxy productProxy) {
        productProxy.setIsDelete(0);
        int count = this.productProxyDao.insert(productProxy);
        return count > 0;
    }
    /**
     * 通过主键删除批量数据
     * @return 是否成功
     */
    @Override
    public boolean delete(Long productId, String proxyIds) {
        List<Long> proxyIdList = Arrays.stream(proxyIds.split(","))
                .map(String::trim) // 去除可能的空白字符
                .map(Integer::parseInt)
                .map(Integer::longValue)// 将字符串转换为整数
                .collect(Collectors.toList());
        Map<String, Object> params = new HashMap<>();
        params.put("productId", productId);
        params.put("proxyIdList", proxyIdList);
        this.productProxyDao.delete(params);
        return false;
    }

    @Override
    public Boolean update(long productId, String proxyIds) {
        List<Long> proxyIdList = Arrays.stream(proxyIds.split(","))
                .map(String::trim) // 去除可能的空白字符
                .map(Integer::parseInt)
                .map(Integer::longValue)// 将字符串转换为整数
                .collect(Collectors.toList());
        for (Long proxyId : proxyIdList) {
            ProductProxy productProxy = new ProductProxy();
            productProxy.setProductId(productId);
            productProxy.setProxyId(proxyId);
            productProxy.setIsDelete(1);
            productProxyDao.update(productProxy);
        }
        return null;
    }

    @Override
    public Result addProxy(ProductProxyDTO productProxyDTO) {
        ProductProxy productProxy = new ProductProxy();
        BeanUtils.copyProperties(productProxyDTO,productProxy);
        productProxy.setIsDelete(0);
        this.productProxyDao.insert(productProxy);
        return Result.success();
    }

    @Override
    public Result deleteProxy(Long productId) {
        int count = productProxyDao.deleteByProductId(productId);
        return Result.success();
    }

    @Override
    public List<Long> getProxy(Long productId) {
        List<ProductProxy> productProxyList = productProxyDao.queryById(productId);
        List<Long> proxyIdList = new ArrayList<>();
        for (ProductProxy productProxy : productProxyList) {
            proxyIdList.add(productProxy.getProxyId());
        }
        return proxyIdList;
    }
}
